import 'dart:convert';

import 'package:development_assistant/config/dio_config.dart';
import 'package:development_assistant/model/api_info.dart';
import 'package:development_assistant/service/api_service.dart';
import 'package:development_assistant/service/env_service.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class ApiController extends GetxController{


  var apiID=0.obs;
  var apiName="".obs;
  var apiMethod="GET".obs;
  var apiUrl="".obs;
  var apiDate="".obs;
  var apiEnv="测试环境".obs;
  var apiParams=[
    {
      "name":"",
      "param":""
    }
  ].obs;
  var apiHeaders=[
    {
      "header":"",
      "param":""
    }
  ].obs;
  var apiBody="".obs;
  var result="".obs;
  var resultCode=0.obs;
  var apiList=RxList<ApiInfo>();

  ApiService apiService=ApiService();
  EnvService envService=EnvService();

  Future getApiList() async{
    List<Map<String,dynamic>> list=await apiService.getApiList();;
    apiList.assignAll(list.map((e) =>ApiInfo.fromJson(e)).toList());
  }

  Future addApi() async{
    String addDate = DateFormat('yyyy-MM-dd HH:mm').format(DateTime.now());
    ApiInfo apiInfo=ApiInfo(null,apiName.value, apiMethod.value, apiUrl.value, addDate);
    await apiService.addApi(apiInfo);
    Get.snackbar("保存接口".tr, "${'成功保存接口'.tr} : ${apiName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":1});
  }

  Future updateApi() async{
    ApiInfo apiInfo=ApiInfo(apiID.value,apiName.value, apiMethod.value, apiUrl.value, apiDate.value);
    await apiService.updateApi(apiInfo);
    Get.snackbar("修改接口".tr, "${'成功修改接口'.tr} : ${apiName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":1});
  }

  Future delApi() async{
    await apiService.delApi(apiID.value);
    Get.snackbar("删除接口".tr, "${'成功删除接口'.tr} : ${apiName.value}");
    Get.offNamed("/Home",arguments: {"selectIndex":1});
  }

  Future sendRequest() async {
    resultCode.value=0;
    result.value="";
    var envResult=await envService.envInfo(apiEnv.value.tr);
    DioConfig dioConfig=DioConfig(envResult[0]['env_url']);
    Map<String,dynamic> paramsList={};
    Map<String,String> headersList={};
    for (var param in apiParams){
      if(param["name"]!=""){
        paramsList[param["name"]!]=param["param"];
      }
    }
    for (var header in apiHeaders){
      if(header["header"]!=""){
        headersList[header["header"]!]=header["param"]!;
      }
    }
    try{
      var response;
      switch (apiMethod.value){
        case "GET":
          response=await dioConfig.get(apiUrl.value, paramsList,apiBody.value, headersList);
          break;
        case "POST":
          response=await dioConfig.post(apiUrl.value, paramsList,apiBody.value, headersList);
          break;
        case "DELETE":
          response=await dioConfig.delete(apiUrl.value, paramsList,apiBody.value, headersList);
          break;
        case "PUT":
          response=await dioConfig.put(apiUrl.value, paramsList,apiBody.value, headersList);
          break;
      }
      resultCode.value=response.statusCode;
      result.value=response.data.toString();
      Get.snackbar("快速请求".tr, "${'发送请求成功'.tr} : ${apiUrl.value}");
    }catch(e){
      resultCode.value=400;
      result.value=e.toString();
      Get.snackbar("快速请求".tr, "${'发送请求失败'.tr} : ${apiUrl.value}");
    }

  }

}